/**
 * SpringMVC restful 风格
 * 共用控制器，控制关于共用的一些业务逻辑操作
 * @author yanwl
 * @date 2014-09-15
 */
package com.chnbst.bstyun.controller.user;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chnbst.bstyun.core.common.DESUtil;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.Order;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/eos")
public class EosController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private Eos eos;
	
	private static final String ROLE_COMPANY = "ROLE_COMPANY";//公司用户
	private static final String ROLE_PERSON = "ROLE_PERSON";//个人用户
	/**
	 * post请求 接受普元用户  http://172.16.3.150/eos/receiveUser
	 * @param request
	 * @return map
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/receiveUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> receiveUser(User user,HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = (List<User>)userService.emailExists(user.getUserEmail());
		if(list.size() > 0) {
			map.put("message", "用户名已存在");
		}else {
			user.setAccountMoney(0.00);
			user.setAddTime(new Date());
			user.setApprove("1");
			user.setInvoiceMoney(0.00);
			user.setIsLive("1");
			user.setIsYun("2");
			user.setLastLoginTime(new Date());
			user.setLoginNumber("0");
			user.setRoleId(ROLE_PERSON);
			user.setUserMessageNum(0);
			user.setUserPassword(DESUtil.encrypt(user.getUserPassword()));
			user.setUserValid("0");
			
			boolean isSuccess = false;
			try {
				isSuccess = userService.insertUser(user);
			} catch (Exception e) {
				isSuccess = false;
			}
			if(isSuccess) {
				map.put("message", "ok");
			}else {
				map.put("message", "新增用户失败，请重试！！");
			}
		}
		return map;
	}
	
	/**
	 * post请求 更新普元用户信息  http://172.16.3.150/eos/updateUser
	 * @param request
	 * @return map
	 */
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateUser(HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String userEmail = request.getParameter("userEmail");
		String userPassword = request.getParameter("userPassword");
		String phone = request.getParameter("phone");
		String realName = request.getParameter("realName");
		String belongCompanyName = request.getParameter("belongCompanyName");
		String companyDepartment = request.getParameter("companyDepartment");
		String virtualPhone = request.getParameter("virtualPhone");
		
		@SuppressWarnings("unchecked")
		List<User> list = (List<User>)userService.emailExists(userEmail);
		if(list.size() > 0) {
			User user = list.get(0);
			
			if(SignUtil.isNotEmpty(userPassword) && !"null".equalsIgnoreCase(userPassword)) {
				user.setUserPassword(DESUtil.encrypt(userPassword));
			}
			user.setPhone(phone);
			if(SignUtil.isNotEmpty(realName) && !"null".equalsIgnoreCase(realName)) {
				user.setRealName(realName);
			}
			if(SignUtil.isNotEmpty(belongCompanyName) && !"null".equalsIgnoreCase(belongCompanyName)) {
				user.setBelongCompanyName(belongCompanyName);
			}
			if(SignUtil.isNotEmpty(companyDepartment) && !"null".equalsIgnoreCase(companyDepartment)) {
				user.setCompanyDepartment(companyDepartment);
			}
			if(SignUtil.isNotEmpty(virtualPhone) && !"null".equalsIgnoreCase(virtualPhone)) {
				user.setVirtualPhone(virtualPhone);
			}
			
			boolean isSuccess = false;
			try {
				isSuccess = userService.updateUserByKey(user);
			} catch (Exception e) {
				isSuccess = false;
			}
			if(isSuccess) {
				map.put("message", "ok");
			}else {
				map.put("message", "修改用户失败，请重试！！");
			}
		}else {
			map.put("message", "该用户不存在，无法进行修改！！");
		}
		return map;
	}
	
	
	/**
	 * post请求 删除普元用户  http://172.16.3.150/eos/deleteUser
	 * @param request
	 * @return map
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteUser(HttpServletRequest request) throws Exception {
		String userEmail = request.getParameter("userEmail");
		Map<String, Object> map = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		List<User> list = (List<User>)userService.emailExists(userEmail);
		if(list.size() > 0) {
			User user = list.get(0);
			
			if(SignUtil.isNotEmpty(user.getBoundEmail()) || SignUtil.isNotEmpty(user.getBoundTel())) {
				if(SignUtil.isNotEmpty(user.getBoundEmail())) {
					user.setUserEmail(user.getBoundEmail());
				}else {
					user.setUserEmail("");
					user.setUserTel(user.getBoundTel());
				}
				user.setVirtualPhone("");
				user.setCompanyDepartment("");
				user.setBelongCompanyName("");
				user.setCompanyDomainName("");
				if(user.getRoleId().equals(ROLE_COMPANY)) {
					user.setRoleId(ROLE_PERSON);
					user.setIsYun("2");
				}
			}else {
				user.setUserValid("1");
				user.setIsLive("0");
			}
			
			boolean isSuccess = false;
			try {
				isSuccess = userService.updateUserByKey(user);
			} catch (Exception e) {
				isSuccess = false;
			}
			if(isSuccess) {
				map.put("message", "ok");
			}else {
				map.put("message", "删除用户失败，请重试！！");
			}
		}else {
			map.put("message", "该用户不存在，无法进行删除！！");
		}
		return map;
	}
	
	
	/**
	 * post请求 开通应用
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "/dredgeApply", method = RequestMethod.POST)
	@ResponseBody
	public boolean dredgeApply(HttpServletRequest request) throws Exception {
		boolean isSuccess = false;
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		Order order = orderService.selectOrderByOrderId(orderId);
		Goods goods = goodsService.selectByGoodsId(order.getGoodsId());
		String result = "";
		if(order.getBuyType() == 0) {
			if("0".equals(goods.getGoodsRalateGoods())) {
				result = eos.sendMeal(order);
			}else {
				result = eos.sendProduct(order);
			}
		}else {
			result = eos.sendProOrMeal(order);
		}
		
		if("ok".equalsIgnoreCase(result)) {
			order.setIsDredge(1);
			orderService.updateOrderByOrderId(order);
			isSuccess = true;
		}
		return isSuccess;
	}
}
